package com.android.accessibility.function;

import java.util.List;

import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.graphics.Rect;
import android.os.Parcelable;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.android.accessibility.utils.Constant;

public class WechatGrabAccessibility extends Accessibility {

    private static final CharSequence LauncherUI = "com.tencent.mm.ui.LauncherUI";
    private static final CharSequence LuckyMoneyReceiveUI = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI";
    private static final CharSequence LuckyMoneyDetailUI = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI";

    public WechatGrabAccessibility(AccessibilityService service) {
        super(service);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            if (Constant.WX_PACKAGENAME.equals(event.getPackageName())) {
                clickNotification(event);
            }
        } else if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            if (LauncherUI.equals(mClassName)) {
                fetchPackage(event);
            } else if (LuckyMoneyReceiveUI.equals(mClassName)) {
                openPackage(event);
            }
        } else if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
            if (LauncherUI.equals(mClassName)) {
                fetchPackage(event);
            } else if (LuckyMoneyReceiveUI.equals(mClassName)) {
                openPackage(event);
            }
        }
    }

    private boolean clickNotification(AccessibilityEvent event) {
        List<CharSequence> textList = event.getText();
        if (textList != null && !textList.isEmpty()) {
            for (CharSequence c : textList) {
                String content = c.toString();
                if (content.contains("[微信红包]")) {
                    Parcelable parcelable = event.getParcelableData();
                    if (parcelable != null
                            && parcelable instanceof Notification) {
                        Notification notification = (Notification) event
                                .getParcelableData();
                        PendingIntent pendingIntent = notification.contentIntent;
                        try {
                            pendingIntent.send();
                            return true;
                        } catch (CanceledException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return false;
    }

    private void fetchPackage(AccessibilityEvent event) {
        List<AccessibilityNodeInfo> clickNodeList = Helper.findClickableNodeListByText(getService(), "领取红包");
        if (clickNodeList != null && clickNodeList.size() > 0) {
            clickNodeList.get(clickNodeList.size() - 1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }
    }

    private boolean openPackage(AccessibilityEvent event) {
        List<AccessibilityNodeInfo> list = Helper
                .findClickableNodeInfoList(event.getSource());
        if (list != null) {
            AccessibilityNodeInfo findedNodeInfo = null;
            Rect outBounds = new Rect();
            for (AccessibilityNodeInfo info : list) {
                info.getBoundsInParent(outBounds);
                if (outBounds.width() > 80) {
                    findedNodeInfo = info;
                    break;
                }
            }
            if (findedNodeInfo != null) {
                findedNodeInfo
                        .performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }
        return false;
    }

}
